package com.krmes.mes;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.krmes.mes.app.AppContext;
import com.krmes.mes.app.AppManager;

import cn.hugo.android.scanner.CaptureActivity;

public class MainActivity extends FragmentActivity
        implements View.OnClickListener {

    private FrameLayout dataFl;
    private ImageView dataIv;
    private FragmentStartData fragmentData;
    private FragmentMe fragmentMy;
    private FrameLayout myFl;
    private ImageView myIv;
    private ImageView toggleImageView;
    private ImageView checkInImageView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        AppManager.getAppManager().addActivity(this);
        initView();
        initData();
//        if (AppContext.roleCode.equals("workman"))
//        {
//            clickDataBtn();
//            return;
//        }else {
//            clickMyBtn();
//            return;
//        }
        clickMyBtn();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.layout_data: /*2131230773*/
                if (AppContext.roleCode.contains("workman")) {
                    clickDataBtn();
                } else {
                    AppContext.showToast("只有报工角色才有权限查看");
                }
                break;
            case R.id.layout_my:
                clickMyBtn();
                break;
            //登记
            case R.id.toggle_btn2:
                if (AppContext.roleCode.equals("workman")) {//if (AppContext.roleCode.contains("workman")) {
                    clickToggleBtn(1);
                } else {
                    AppContext.showToast("只有报工角色才有权限查看");
                }
                break;
            //报工   20170301不在支持检验 检验移至我的界面功能组
            case R.id.toggle_btn:
                if (AppContext.roleCode.equals("workman")) {//if (AppContext.roleCode.contains("workman")) {
                    clickToggleBtn(2);
                    //showNormalDialog();
                } else {
                    AppContext.showToast("只有报工角色才有权限查看");
                }
                break;
            default:
                break;
        }
    }


    @Override
    public boolean onKeyDown(int paramInt, KeyEvent paramKeyEvent)
    {
        if ((paramInt == 4) && (paramKeyEvent.getRepeatCount() == 0)) {
            return true;
        }
        return super.onKeyDown(paramInt, paramKeyEvent);
    }


    private void clickDataBtn()
    {
        this.fragmentData = new FragmentStartData();
        getSupportFragmentManager().beginTransaction().replace(R.id.frame_main, this.fragmentData).commit();
        this.dataFl.setSelected(true);
        this.dataIv.setSelected(true);
        this.myFl.setSelected(false);
        this.myIv.setSelected(false);
    }

    private void clickMyBtn()
    {
        this.fragmentMy = new FragmentMe();
        getSupportFragmentManager().beginTransaction().replace(R.id.frame_main, this.fragmentMy).commit();
        this.dataFl.setSelected(false);
        this.dataIv.setSelected(false);
        this.myFl.setSelected(true);
        this.myIv.setSelected(true);
    }



    private void clickToggleBtn(int fun)
    {
        Intent intent = new Intent(this, CaptureActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);//Intent.FLAG_RECEIVER_REGISTERED_ONLY
        intent.putExtra("fun",fun);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);//关掉所要到的界面中间的activity
        //intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);//设置不要刷新将要跳转的界面

        //intent.getIntExtra("fun",2);
        //Intent.ACTION_APPLICATION_RESTRICTIONS_CHANGED
        startActivity(intent);
    }

    private void initData()
    {
        this.dataFl.setOnClickListener(this);
        this.myFl.setOnClickListener(this);
        this.toggleImageView.setOnClickListener(this);
        this.checkInImageView.setOnClickListener(this);
    }

    private void initView()
    {
        this.dataFl = ((FrameLayout)findViewById(R.id.layout_data));
        this.myFl = ((FrameLayout)findViewById(R.id.layout_my));
        this.dataIv = ((ImageView)findViewById(R.id.image_data));
        this.myIv = ((ImageView)findViewById(R.id.image_my));
        this.toggleImageView = ((ImageView)findViewById(R.id.toggle_btn));
        this.checkInImageView = ((ImageView)findViewById(R.id.toggle_btn2));
    }

}
